//
//  FDApplyView.m
//  FamousDoctorOnline
//
//  Created by Cain on 16/7/7.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDApplyView.h"
#import "FDApplyModel.h"
#import "UILabel+FDExtension.h"

@interface FDApplyView ()

//用户头像
@property (nonatomic , weak)UIImageView *userImage;
//病人文本框
@property (nonatomic , weak)UILabel *patientLabel;
//就诊时间
@property (nonatomic , weak)UILabel *timeLabel;
//审核按钮
@property (nonatomic , weak)UIButton *checkButton;
//分割线
@property (nonatomic , weak)UIView *lineView;
//预约医生
@property (nonatomic , weak)UITextField *appointmentDoctor;
//医生
@property (nonatomic , weak)UILabel *doctorLabel;
//医院
@property (nonatomic , weak)UILabel *hospitalLabel;
//预约通知
@property (nonatomic , weak)UITextField *appointmentMessager;
//就诊地点
@property (nonatomic , weak)UILabel *locationLabel;
//取号时间
@property (nonatomic , weak)UILabel *recaptionLabel;
//取号地址
@property (nonatomic , weak)UILabel *placeLabel;
//短信重发
@property (nonatomic , weak)UIButton *messageButton;
//取消预约
@property (nonatomic , weak)UIButton *cancleButton;
//递交反馈
@property (nonatomic , weak)UIButton *backButton;

@end

@implementation FDApplyView

/**赋值模型数据*/
- (void)setApplyModel:(FDApplyModel *)applyModel
{
    _applyModel = applyModel;
    
    //创建不需要模型数据的控件
    [self setupUI];
    
    //病人文本框
    UILabel *patientLabel = [UILabel setLightGrayColorLabelWithString:_applyModel.patients];
    self.patientLabel = patientLabel;
    //赋值可变属性字符串
    patientLabel.attributedText = [self setMutableAttributesStringWithString:patientLabel range:NSMakeRange(0, 3)];
    [self addSubview:patientLabel];
    
    //就诊时间文本框
    UILabel *timeLabel = [UILabel setLightGrayColorLabelWithString:_applyModel.time];
    self.timeLabel = timeLabel;
    [self addSubview:timeLabel];
    
    //审核按钮
    UIButton *checkButton = [[UIButton alloc]init];
    self.checkButton = checkButton;
    [checkButton setTitle:_applyModel.check forState:UIControlStateNormal];
    [checkButton setBackgroundColor:[UIColor colorWithRed:40/255.0 green:130/255.0 blue:180/255.0 alpha:1]];
    checkButton.layer.cornerRadius = SCREEN_WIDTH*0.1;
    checkButton.clipsToBounds = YES;
    [self addSubview:checkButton];
    
    //医生
    UILabel *doctorLabel = [UILabel setLightGrayColorLabelWithString:_applyModel.doctor];
    self.doctorLabel = doctorLabel;
    doctorLabel.attributedText = [self setMutableAttributesStringWithString:doctorLabel range:NSMakeRange(0, 3)];
    [self addSubview:doctorLabel];
    
    //医院
    UILabel *hospitalLabel = [UILabel setLightGrayColorLabelWithString:_applyModel.hospital];
    self.hospitalLabel = hospitalLabel;
    [self addSubview:hospitalLabel];
    
    //就诊地点
    NSString *location = [NSString stringWithFormat:@"就诊地点:  %@%d号楼肛肠科",_applyModel.hospital,arc4random_uniform(10)];
    UILabel *locationLabel = [UILabel setLightGrayColorLabelWithString:location];
    self.locationLabel = locationLabel;
    locationLabel.attributedText = [self setMutableAttributesStringWithString:locationLabel range:NSMakeRange(0, 5)];
    [self addSubview:locationLabel];
    
    //取号地址
    NSString *placeString = [NSString stringWithFormat:@"取号地址: %@住院部",_applyModel.hospital];
    UILabel *placeLabel = [UILabel setLightGrayColorLabelWithString:placeString];
    self.placeLabel = placeLabel;
    placeLabel.attributedText = [self setMutableAttributesStringWithString:placeLabel range:NSMakeRange(0, 5)];
    [self addSubview:placeLabel];
}

/**不需要模型数据的控件创建*/
- (void)setupUI {
    
    //用户头像
    UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"banner"]];
    self.userImage = userImage;
    [self addSubview:userImage];
    
    //预约医生
    UITextField *appointmentDoctor = [self setCustomTextField:@"  预约医生:"];
    self.appointmentDoctor = appointmentDoctor;
    [self addSubview:appointmentDoctor];
    
    //预约通知
    UITextField *appointmentMessage = [self setCustomTextField:@"预约通知:"];
    self.appointmentMessager = appointmentMessage;
    [self addSubview:appointmentMessage];
    
    //取号时间
    UILabel *recaptionLabel = [UILabel setLightGrayColorLabelWithString:@"取号时间: 9:00 - 10.00"];
    self.recaptionLabel = recaptionLabel;
    recaptionLabel.attributedText = [self setMutableAttributesStringWithString:recaptionLabel range:NSMakeRange(0, 5)];
    [self addSubview:recaptionLabel];
    
    //分割线
    UIView *lineView = [[UIView alloc]init];
    self.lineView = lineView;
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView];
    
    //短信重发
    UIButton *messagerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.messageButton = messagerButton;
    [messagerButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [messagerButton setTitle:@"短信重发" forState:UIControlStateNormal];
    [messagerButton setBackgroundImage:[UIImage imageNamed:@"link_button_02"] forState:UIControlStateNormal];
    [self addSubview:messagerButton];
    
    //取消预约
    UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancleButton = cancleButton;
    [cancleButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [cancleButton setTitle:@"取消预约" forState:UIControlStateNormal];
    [cancleButton setBackgroundImage:[UIImage imageNamed:@"link_button_02"] forState:UIControlStateNormal];
    [self addSubview:cancleButton];
    
    //递交反馈
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton = backButton;
    [backButton setBackgroundImage:[UIImage imageNamed:@"dijiaofankui"] forState:UIControlStateNormal];
    [self addSubview:backButton];
    
}


/**布局子控件*/
- (void)layoutSubviews
{
    [super layoutSubviews];
    //用户头像约束
    [self.userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(80);
        make.left.offset(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH*0.2, SCREEN_WIDTH*0.2));
    }];
    
    //病人文本框约束
    [self.patientLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userImage.mas_right).offset(20);
        make.top.mas_equalTo(self.userImage.mas_top);
    }];
    
    //就诊时间文本框约束
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.patientLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(self.patientLabel.mas_left);
    }];
    
    //审核按钮约束
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-20);
            make.centerY.mas_equalTo(self.userImage.mas_centerY);
            make.size.mas_equalTo(self.userImage.size);
        }];
    });
    
    //预约医生约束
    [self.appointmentDoctor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userImage.mas_bottom).offset(10);
        make.left.offset(-3);
        make.width.mas_equalTo(self.width+6);
        make.height.mas_equalTo(SCREEN_WIDTH*0.1);
    }];
    
    //医生约束
    [self.doctorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.appointmentDoctor.mas_bottom).offset(10);
        make.left.offset(10);
    }];
    
    //医院
    [self.hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.doctorLabel.mas_bottom).offset(10);
        make.left.offset(10);
    }];
    
    //预约通知
    [self.appointmentMessager mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.hospitalLabel.mas_bottom).offset(10);
        make.left.offset(-3);
        make.width.mas_equalTo(self.width+6);
        make.height.mas_equalTo(self.appointmentDoctor.mas_height);
    }];
    
    //就诊地点
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.appointmentMessager.mas_bottom).offset(10);
        make.left.offset(10);
    }];
    
    //取号时间
    [self.recaptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.locationLabel.mas_bottom).offset(15);
        make.left.mas_equalTo(self.locationLabel);
    }];
    
    //取号地址
    [self.placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.recaptionLabel.mas_bottom).offset(15);
        make.left.mas_equalTo(self.recaptionLabel);
    }];
    
    //分割线约束
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(self.placeLabel.mas_bottom).offset(10);
        make.left.right.mas_equalTo(0);
    }];
    
    //短信重发
    [self.messageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.bottom.offset(-20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH*0.4, SCREEN_WIDTH*0.15));
    }];
    
    //取消预约
    [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.offset(-20);
        
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH*0.4, SCREEN_WIDTH*0.15));
    }];
    
    //递交反馈
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.checkButton.mas_right);
        make.top.mas_equalTo(self.appointmentDoctor.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH*0.2, SCREEN_WIDTH*0.1));
    }];
    
}

/**传入一个label,范围字符串的字体颜色为自定义的颜色*/
- (NSMutableAttributedString *)setMutableAttributesStringWithString:(UILabel *)label range:(NSRange)range
{
    //范围字符串属性修改
    NSDictionary *attributes = @{NSForegroundColorAttributeName:label.textColor
                                 };
    //1.范围截取字符串
    NSString *rangeString = [label.text substringWithRange:range];
    //2.将字符串改为可变属性字符串
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:label.text attributes:attributes];
    //3.设置属性和需要修改的文字范围
    UIColor *black = [UIColor blackColor];
    NSRange redTextRange = [label.text rangeOfString:rangeString];
    //4.赋值给可变属性字符串
    [attributedString setAttributes:@{NSForegroundColorAttributeName:black} range:redTextRange];
    return attributedString;
}



/**自定义textField*/
- (UITextField *)setCustomTextField:(NSString *)text
{
    UITextField *field = [[UITextField alloc]init];
    field.borderStyle =UITextBorderStyleRoundedRect;
    field.placeholder = text;
    field.font = [UIFont systemFontOfSize:SCREEN_WIDTH*0.05];
    field.userInteractionEnabled =NO;
    return field;
}

@end
